<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTenantTypes">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tenantTypes"
				  highlight-current-row v-loading="listLoading"
				  @selection-change="selsChange" style="width: 100%;">

			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称"  sortable>
			</el-table-column>
			<el-table-column prop="description" label="描述" sortable>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="rows"
						   :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增或编辑" v-model="tenantTypeFormVisible" :close-on-click-modal="false">
			<el-form :model="tenantTypeForm" label-width="80px" :rules="tenantTypeFormRules" ref="tenantTypeForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="tenantTypeForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="description">
					<el-input v-model="tenantTypeForm.description" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="tenantTypeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="submit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					keyword: ''
				},
				tenantTypes: [],	//列表数据
				total: 0,
				page: 1,
				rows: 10 ,
				listLoading: false,
				sels: [],//列表选中列

				tenantTypeFormVisible: false,//新增界面是否显示
				addLoading: false,
				tenantTypeFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					],
					description: [
						{ required: true, message: '请输入描述', trigger: 'blur' }
					],
				},
				//新增界面数据
				tenantTypeForm: {
					name: '',
					description: '',
				}

			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			//提交分页
			handleCurrentChange(val) {
				this.page = val;
				this.getTenantTypes();
			},
			//获取用户列表
			getTenantTypes() {
				let para = {
					page: this.page,	//分页，当前页
					rows: this.rows,	//每页条数
					keyword: this.filters.keyword	//查询条件
				};
				this.listLoading = true;
				this.$http.post("/sysmanage/tenantType/list",para).then(res=>{
					var result = res.data;
					this.tenantTypes = result.rows;
					this.total = result.total;
					this.listLoading = false;

				}).catch(error => {
					this.listLoading = false;
					this.$message({ message: '服务器异常['+error.message+']', type: 'error' });
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/sysmanage/tenantType/"+row.id,{}).then(res=>{
						let result = res.data ;
						if(result.success){
							this.$message({message:'删除成功',type: 'success'});
							this.getTenantTypes();
						}else{
							this.$message({message: result.message,type: 'error'});
						}
						this.listLoading = false;
					}).catch(error => {
						this.$message({message: '删除失败['+error.message+']',type: 'error'});
						this.listLoading = false;
					});

				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.tenantTypeFormVisible = true;
				//id回显
				this.tenantTypeForm = Object.assign({}, row);

			},
			//显示新增界面
			handleAdd: function () {
				this.tenantTypeFormVisible = true;
				this.tenantTypeForm = {
					name: '',
					description: ''
				};

			},
			//新增
			submit: function () {
				this.$refs.tenantTypeForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							//按钮加载状态
							this.addLoading = true;
							//准备参数
							let para = Object.assign({}, this.tenantTypeForm);
							this.$http.put("/sysmanage/tenantType",para).then(res=>{
								let result = res.data ;
								if(result.success){
									this.$message({message:'保存成功',type: 'success'});
									this.$refs['tenantTypeForm'].resetFields();
									this.tenantTypeFormVisible = false;
									this.getTenantTypes();
								}else{
									this.$message({message: result.message,type: 'error'});
								}
								this.addLoading = false;
							}).catch(error => {
								this.$message({message: '保存失败['+error.message+']',type: 'error'});
								this.addLoading = false;
							});

						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id); // [1,2,3]

				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.patch("/sysmanage/tenantType",ids).then(res=>{
						let result = res.data ;
						if(result.success){
							this.$message({message:'删除成功',type: 'success'});
							this.getTenantTypes();
						}else{
							this.$message({message: result.message,type: 'error'});
						}
						this.listLoading = false;
					}).catch(error => {
						this.$message({message: '删除失败['+error.message+']',type: 'error'});
						this.listLoading = false;
					});

				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getTenantTypes();
		}
	}

</script>

<style scoped>

</style>